<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;

class ApiKey extends Model
{
    public function __construct()
    {
        $this->key = Uuid::uuid1()->toString();
        $this->secret = Str::random(15);
    }
    //增加软删除的方法
    use SoftDeletes;

    // protected $dates = ['deleted_at'];
    //隐藏的字段
    protected $hidden=[];
    // 受保护的字段
    protected $guards=[];

    public function customer()
    {
        return $this->belongsTo('App\Models\Customer');
    }

    public function apigateways()
    {
        return $this->hasMany('App\Models\ApiGateway', 'apikey_id');
    }

    public static function findByKey(string $key)
    {
        return (new self)->where(compact('key'))->first();
    }
}
